package com.netnovelstore.annotation.impl;

import com.netnovelstore.pojo.Exception.NotDataException;
import com.netnovelstore.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
@Order(2)
public class NotNullAndBlankImpl {

    private RedisUtil redisUtil;

    @Autowired
    public NotNullAndBlankImpl(RedisUtil redisUtil) {
        this.redisUtil = redisUtil;
    }

    @Pointcut("@annotation(com.netnovelstore.annotation.NotNullAndBlank)")
    public void interPoint() {
    }

    //利用前置增强检查注解
    @Before("interPoint()")
    public void Processing(JoinPoint joinPoint) throws Throwable {
        MethodSignature method = (MethodSignature) joinPoint.getSignature();

        log.info(String.format("[%s] >>>>>>>>>", method.getName()));
        //获取参数列表
        Object[] args = joinPoint.getArgs();
        Class[] parameterTypes = method.getParameterTypes();
        String[] parameterNames = method.getParameterNames();
        for (int i = 0; i < args.length; i++) {
            if (args[i] == null) {
                throw new NotDataException(String.format("[%s] >> [%s] is null", method.getName(), parameterNames[i]));
            }
            if (parameterTypes[i].equals(String.class)) {
                if ("".equals(((String) args[i]).trim())) {
                    throw new NotDataException(String.format("[%s] >> [%s] is blank", method.getName(), parameterNames[i]));
                }
            }
        }
        log.info("run NotNullAndBlankImpl");
//        return (Result) joinPoint.proceed(args);

    }
}
